package com.wteam.framework.modules.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.community.entity.SdModel;
import com.wteam.framework.modules.community.entity.SdModelMark;
import com.wteam.framework.modules.community.mapper.SdModelMapper;
import com.wteam.framework.modules.community.mapper.SdModelMarkMapper;
import com.wteam.framework.modules.community.service.SdModelMarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (SdModelMark)表服务实现类
 *
 * @author doncic
 * @since 2023-07-14 16:59:55
 */
@Service("sdModelMarkService")
public class SdModelMarkServiceImpl extends ServiceImpl<SdModelMarkMapper, SdModelMark> implements SdModelMarkService {

    @Autowired
    private SdModelMarkMapper sdModelMarkMapper;
    @Autowired
    private SdModelMarkService sdModelMarkService;
    @Autowired
    private SdModelMapper sdModelMapper;

    @Override
    public ResultMessage getAllModelMark() {
        // 分页查询所有书签
        Page<SdModelMark> page = new Page<>(1, 10);
        QueryWrapper<SdModelMark> wrapper = new QueryWrapper<>();
        // 执行分页查询并返回结果
        return ResultUtil.data(sdModelMarkMapper.selectPage(page, wrapper));
    }

    @Override
    public boolean addModelMark(SdModelMark sdModelMark) {
        //添加书签是要先登录
        return sdModelMarkService.save(sdModelMark);
    }

    @Override
    public ResultMessage deleteModelMark(Long id) {
        // 创建一个 LambdaQueryWrapper 对象
        LambdaQueryWrapper<SdModelMark> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 设置删除条件，根据主键字段 id 进行匹配
        lambdaQueryWrapper.eq(SdModelMark::getId, id);
        int affectedRows = sdModelMarkMapper.delete(lambdaQueryWrapper);
        // 根据删除操作受影响的行数判断是否删除成功
        return affectedRows > 0
                ? ResultUtil.success()
                : ResultUtil.error(90007, "删除书签失败!");
    }

    @Override
    public ResultMessage findModelByMark(String markName) {
        LambdaQueryWrapper<SdModelMark> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SdModelMark::getMarkName, markName);
        List<SdModelMark> sdModelMarks = sdModelMarkMapper.selectList(lambdaQueryWrapper);
        if (sdModelMarks != null) {
            List<Long> collect = sdModelMarks.stream()
                    .map(SdModelMark::getId)
                    .collect(Collectors.toList());
            //拿出id查找模型
            LambdaQueryWrapper<SdModel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(SdModel::getMarkId, collect);
            List<SdModel> sdModels = sdModelMapper.selectList(queryWrapper);
            //要判断是否为空
            if (!sdModels.isEmpty()) {
                return ResultUtil.data(sdModels);
            }
        }
        return ResultUtil.error(90009, "找不到对应书签的模型");
    }
}